<?php
define('LEGAL_OPER', true);
require('../../includes/admin-init.php');
require(ADMIN_PATH . '/includes/lib_data_list.php');

//列表页
if($method == 'list'){
	$db= new DB();
	$userTable = new BaseDao($db->getDb(),'admin_user','user_id');
	
	$paras = array();
	$sql = DataList::getWhereSqlAndParas($_GET,$db->getDb(),'admin_user',$paras);
	
	$count = $userTable->getCount($sql, $paras);
	$total = 0;
	$page = isset($_GET['page'])?intval($_GET['page']):1;
	$maxresult = isset($_GET['maxresult'])?intval($_GET['maxresult']):10;
	$users = array();
	if($count > 0){
		$page = DataList::getPage($count, $page, $maxresult,&$total);
		$users = $userTable->getScrollData($sql, $paras, DataList::getFirstIndex($page,$maxresult), $maxresult,DataList::getOrderBySql($_GET));
	}
	$roles = getRoles($db);
	if($users != null && count($users) > 0){
		foreach ($users as $key=>$user){
			$users[$key]['rolename'] = $roles['r_'.$user['role_id']][0];
		}
	}
	$db->close();
	
	$smarty->assign("sortfield",$_GET['sort_field']);
	$smarty->assign("sortval",$_GET['sort_val']);
	$smarty->assign("page",$page);
	$smarty->assign("count",$count);
	$smarty->assign("total",$total);
	$smarty->assign("maxresult",$maxresult);
	$smarty->assign("sh",$_GET['sh']);
	$smarty->assign("roles",$roles);
	$smarty->assign("users",$users);
	$smarty->display("auth/admin_list.htm");
	exit;
}

//添加页
if($method == 'addui'){
	$roles = getRoles();
	$smarty->assign("roles",$roles);
	$smarty->display("auth/admin_add.htm");
	exit;
}
//添加
if($method == 'add'){
	$username = $_POST['username'];
	$realname = $_POST['realname'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$roleid = $_POST['roleid'];
	
	if(isLetterDigital($username,6,20)
			&& isEmail($email)
			&& isNotEmpty($realname)
			&& isNotEmpty($phone)){
		
		$sets = array();
		$sets['username'] = trim($username);
		$sets['realname'] = trim($realname);
		$sets['phone'] = trim($phone);
		$sets['email'] = trim($email);
		
		$db= new DB();
		$db->beginTransaction();
		$userTable = new BaseDao($db->getDb(),'admin_user','user_id');
		try{
			$sets = array();
			$sets['username'] = $username;
			$sets['realname'] = $realname;
			$sets['phone'] = $phone;
			$sets['email'] = $email;
			if($roleid > 0){
				$sets['role_id'] = $roleid;
			}
			$sets['password'] = md5('88888888');
			$userTable->save($sets);
			$db->commit();
			$url = '/sys-admin/action/auth/admin.php?met=addsuccess';
			to_redirect($url);
		}catch(Exception $ex){
			$db->rollback();
			$url = '/sys-admin/action/auth/admin.php?met=addfail';
			to_redirect($url);
		}
		$db->close();
	}else{
		$links=array();
		$links[0]['text'] = '管理员列表';
		$links[0]['href'] = '/sys-admin/action/auth/admin.php?met=list';
		to_message($smarty,"数据不正确",1,$links);
	}
	exit;
}


if($method == 'addsuccess'){
	$links[0]['text'] = "管理员添加";
	$links[0]['href'] = '/sys-admin/action/auth/admin.php?met=addui';
	$links[1]['text'] = '管理员列表';
	$links[1]['href'] = '/sys-admin/action/auth/admin.php?met=list';
	to_message($smarty, "管理员添加成功",0,$links);
}
if($method == 'addfail'){
	$links[0]['text'] = "角管理员添加";
	$links[0]['href'] = '/sys-admin/action/auth/admin.php?met=addui';
	$links[1]['text'] = '管理员列表';
	$links[1]['href'] = '/sys-admin/action/auth/admin.php?met=list';
	to_message($smarty, "管理员添加失败",1,$links);
}


//添加页
if($method == 'editui'){
	$id = to_int($_GET['id']);
	$db= new DB();
	$userTable = new BaseDao($db->getDb(),'admin_user','user_id');
	$user = $userTable->getById($id);
	$roles = getRoles($db);
	$smarty->assign("user",$user);
	$smarty->assign("roles",$roles);
	$smarty->display("auth/admin_edit.htm");
	exit;
}




//修改
if($method == 'edit'){
	$id = to_int($_POST['id']);
	$username = $_POST['username'];
	$realname = $_POST['realname'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$roleid = $_POST['roleid'];
	$isactive = $_POST['isactive'];
	if(isLetterDigital($username,6,20)
			&& isEmail($email)
			&& isNotEmpty($realname)
			&& isNotEmpty($phone)){
		$sets = array();
		$sets['username'] = trim($username);
		$sets['realname'] = trim($realname);
		$sets['phone'] = trim($phone);
		$sets['email'] = trim($email);
		$sets['is_active'] = (isset($isactive)&&$isactive==1)?1:0;
		$sets['modified'] = date('y-m-d H:i:s',time());
		try{
			$db = new DB();
			$db->beginTransaction();
			$userTable = new BaseDao($db->getDb(),'admin_user','user_id');
			$userTable->update($sets, "user_id=?", array($id));
			$db->commit();
			$url = '/sys-admin/action/auth/admin.php?met=editsuccess';
			to_redirect($url);
		}catch(Exception $ex){
			$db->rollback();
			$url = '/sys-admin/action/auth/admin.php?met=editsuccess';
			to_redirect($url);
		}
		$db->close();
	}else{
		$links=array();
		$links[0]['text'] = '管理员列表';
		$links[0]['href'] = '/sys-admin/action/auth/admin.php?met=list';
		to_message($smarty,"数据不正确",1,$links);
	}
	exit;
}

if($method == 'editsuccess'){
	$links[0]['text'] = '管理员列表';
	$links[0]['href'] = '/sys-admin/action/auth/admin.php?met=list';
	to_message($smarty, "管理员修改成功",0,$links);
}
if($method == 'editfail'){
	$links[0]['text'] = '管理员列表';
	$links[0]['href'] = '/sys-admin/action/auth/admin.php?met=list';
	to_message($smarty, "管理员修改失败",1,$links);
}


//删除用户
if($method == 'delete'){
	$id = to_int($_GET['id']);
	$db = new DB();
	$db->beginTransaction();
	$base = new BaseDao($db->getDb(),'admin_user','user_id');
	try{
		$base->delete('user_id=?', array($id));
		$db->commit();
		echo '1';
	}catch(Exception $e){
		$db->rollback();
		echo '0';
	}
	$db->close();
	exit;
}


//获取缓存中的所有角色信息
function getRoles($db=null){
	$roles = array();
		if($db == null){
			$db = new DB();
		}
		$roleTable = new BaseDao($db->getDb(),'admin_role','role_id');
		$roleList = $roleTable->getAll(null,null,array('sort_order asc'));
		if($roleList != null && count($roleList) > 0){
			$roleMapTable = new BaseDao($db->getDb(),'admin_role_action_map','id');
			$map = $roleMapTable->getAll(null, null);
			$actionTable = new BaseDao($db->getDb(),'admin_action','action_id');
			$actionList = $actionTable->getAll('is_active=?',array(1));
			foreach ($roleList as $role){
				$codesStr = '';
				foreach ($map as $entry){
					if($entry['role_id'] == $role['role_id']){
						foreach ($actionList as $action){
							if($action['action_id'] == $entry['action_id']){
								$codesStr = $codesStr.','.$action['action_code'];
							}
						}
					}
				}
				if($codesStr!=''){
					$codesStr = $codesStr.',';
				}
				$roles['r_'.$role['role_id']] = array($role['role_name'],$codesStr,$role['role_id']);
			}
		}
	return $roles;
}



